/*
 * Prefs.java
 *
 * Copyright(c) 2011
 * Michael Kroez
 * kroez@studiorutton.de
 *
 * All rights reserved.
 *
 * $Id$
 * 
 */

package de.studiorutton.gerengdic;

import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.util.Log;
import de.studiorutton.access.FileLocation;
import de.studiorutton.colorschemes.BlueColorSchemeMapper;
import de.studiorutton.colorschemes.CoffeeColorSchemeMapper;
import de.studiorutton.colorschemes.IColorSchemeMapper;
import de.studiorutton.colorschemes.DarkColorSchemeMapper;
import de.studiorutton.colorschemes.GreenColorSchemeMapper;
import de.studiorutton.colorschemes.LightColorSchemeMapper;
import de.studiorutton.colorschemes.NoFrillsColorSchemeMapper;
import de.studiorutton.colorschemes.NoFrillsLightColorSchemeMapper;
import de.studiorutton.colorschemes.PinkColorSchemeMapper;
import de.studiorutton.colorschemes.RedColorSchemeMapper;
import de.studiorutton.colorschemes.TitleStyleDark;
import de.studiorutton.colorschemes.TitleStyleIphone;
import de.studiorutton.colorschemes.TitleStyleLight;
import de.studiorutton.gerengdic.dictionary.Dictionary;
import de.studiorutton.tre.SimpleTre;

public class Prefs {
	private final static String TAG = "learningwords";

	private static Prefs INSTANCE = null;

	private String dictionaryMode = "";
	private String oldDictionaryMode = "";

	private boolean eraseButtonEnabled;
	private boolean oldEraseButtonEnabled;

	private String extStorageDir = "";
	private String fulltextSearchMode = "";

	private boolean aMatchInIndexSearch;
	private boolean oldAMatchInIndexSearch;
	private boolean aMatchInFulltextSearch;
	private boolean oldAMatchInFulltextSearch;

	private IColorSchemeMapper cSchemeMapper;
	private String oldCSchemeValue = "";

	private String oldSearchbarStyleValue = "";

	private boolean indexedInverseEnabled;
	private boolean oldIndexedInverseEnabled;
	private boolean languageButtonColorEnabled;
	private boolean oldLanguageButtonColorEnabled;

	private boolean viewConfigChanged;
	private boolean searchConfigChanged;
	private boolean versionChanged;

	public Prefs() {
	}

	public static Prefs getInstance() {
		if (INSTANCE == null) {
			INSTANCE = new Prefs();
		}

		return INSTANCE;
	}

	public void setPreferences() {
		Resources res = LearningWordsApplication.get().getResources();

		// Get the xml/preferences.xml preferences
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(LearningWordsApplication.get());
		viewConfigChanged = false;
		searchConfigChanged = false;

		dictionaryMode = prefs.getString("dictionarModeDef", "1");
		if (!oldDictionaryMode.equals(dictionaryMode))
			viewConfigChanged = true;
		oldDictionaryMode = dictionaryMode;
		// Log.d( TAG, "dictionary mode is *" + dictionaryMode + "*" );
		eraseButtonEnabled = prefs.getBoolean("eraseButtonPref", false);
		if (oldEraseButtonEnabled != eraseButtonEnabled)
			viewConfigChanged = true;
		oldEraseButtonEnabled = eraseButtonEnabled;

		extStorageDir = prefs.getString("externalStoragePref", "");
		// Log.d( TAG, "external storage dir is *" + getExtStorageDir() + "*" );
		if (!getExtStorageDir().equals(""))
			FileLocation.setExternalStorage(getExtStorageDir());

		fulltextSearchMode = prefs.getString("fulltextSearchModeDef", "0");
		Dictionary.matchFullWordMode = getFulltextSearchMode();

		aMatchInIndexSearch = prefs.getBoolean("aMatchInIndexSearchDef", false);
		aMatchInFulltextSearch = prefs.getBoolean("aMatchInFulltextSearchDef",
				false);

		if (oldAMatchInFulltextSearch != aMatchInFulltextSearch
				|| oldAMatchInIndexSearch != aMatchInIndexSearch)
			searchConfigChanged = true;
		oldAMatchInIndexSearch = aMatchInIndexSearch;
		oldAMatchInFulltextSearch = aMatchInFulltextSearch;

		String cschemeValue = prefs.getString("colorSchemeDef", "1");
		if (cschemeValue.equals("0"))
			cSchemeMapper = new DarkColorSchemeMapper(res);
		else if (cschemeValue.equals("1"))
			cSchemeMapper = new LightColorSchemeMapper(res);
		else if (cschemeValue.equals("2"))
			cSchemeMapper = new RedColorSchemeMapper(res);
		else if (cschemeValue.equals("3"))
			cSchemeMapper = new BlueColorSchemeMapper(res);
		else if (cschemeValue.equals("4"))
			cSchemeMapper = new GreenColorSchemeMapper(res);
		else if (cschemeValue.equals("5"))
			cSchemeMapper = new CoffeeColorSchemeMapper(res);
		else if (cschemeValue.equals("6"))
			cSchemeMapper = new PinkColorSchemeMapper(res);
		else if (cschemeValue.equals("7"))
			cSchemeMapper = new NoFrillsColorSchemeMapper(res);
		else if (cschemeValue.equals("8"))
			cSchemeMapper = new NoFrillsLightColorSchemeMapper(res);
		else
			cSchemeMapper = new NoFrillsColorSchemeMapper(res);

		if (!oldCSchemeValue.equals(cschemeValue))
			viewConfigChanged = true;
		oldCSchemeValue = cschemeValue;

		String searchbarStyleValue = prefs.getString("searchbarStyleDef", "0");
		if (searchbarStyleValue.equals("0"))
			cSchemeMapper.setTitleStyle(new TitleStyleDark(res));
		else if (searchbarStyleValue.equals("1"))
			cSchemeMapper.setTitleStyle(new TitleStyleLight(res));
		else
			cSchemeMapper.setTitleStyle(new TitleStyleIphone(res));

		if (!oldSearchbarStyleValue.equals(searchbarStyleValue))
			viewConfigChanged = true;
		oldSearchbarStyleValue = searchbarStyleValue;

		indexedInverseEnabled = prefs.getBoolean("indexedInversePref", false);
		if (indexedInverseEnabled)
			cSchemeMapper.setIndexedInverse(true);
		else
			cSchemeMapper.setIndexedInverse(false);

		if (oldIndexedInverseEnabled != indexedInverseEnabled)
			viewConfigChanged = true;
		oldIndexedInverseEnabled = indexedInverseEnabled;

		languageButtonColorEnabled = prefs.getBoolean(
				"languabeButtonColorPref", false);
		if (oldLanguageButtonColorEnabled != languageButtonColorEnabled)
			viewConfigChanged = true;
		oldLanguageButtonColorEnabled = languageButtonColorEnabled;

		String versionValue = prefs.getString("versionDef", "v0.0");
		String versionPackageValue;
		try {
			versionPackageValue = LearningWordsApplication.get()
					.getPackageManager()
					.getPackageInfo("de.studiorutton.gerengdic", 0).versionName;
		} catch (NameNotFoundException e) {
			versionPackageValue = "v0.0";
		}
		Log.d(TAG, "versionValue = " + versionValue + " versionPackageValue = "
				+ versionPackageValue);
		if (versionValue.equals("v0.0")
				|| !versionValue.equals(versionPackageValue)) {
			versionChanged = true;
			prefs.edit().putString("versionDef", versionPackageValue).commit();
		}
	}

	public String getDictionaryMode() {
		return dictionaryMode;
	}

	public String getExtStorageDir() {
		return extStorageDir;
	}

	public IColorSchemeMapper getColorSchemeMapper() {
		return cSchemeMapper;
	}

	public boolean getLanguageButtonColor() {
		return languageButtonColorEnabled;
	}

	public boolean getEraseButtonEnabled() {
		return eraseButtonEnabled;
	}

	public int getFulltextSearchMode() {
		if (fulltextSearchMode.equals("0"))
			return SimpleTre.FULL_WORDS;
		else if (fulltextSearchMode.equals("1"))
			return SimpleTre.FULL_BEGINNING;
		else
			return SimpleTre.NO_FULL_WORDS;
	}

	public boolean getAMatchInIndexSearch() {
		return aMatchInIndexSearch;
	}

	public boolean getAMatchInFulltextSearch() {
		return aMatchInFulltextSearch;
	}

	public boolean viewConfigChanged() {
		boolean result = viewConfigChanged;
		viewConfigChanged = false;
		return result;
	}

	public boolean searchConfigChanged() {
		boolean result = searchConfigChanged;
		searchConfigChanged = false;
		return result;
	}

	public boolean versionChanged() {
		boolean result = versionChanged;
		versionChanged = false;
		return result;
	}
}